//
//  MPCTextField.m
//  Pods
//
//  Created by HouGeng on 17/7/22.
//
//

#import "MPCTextField.h"

@interface MPCTextField()

/// 默认文字的大小
@property (nonatomic, strong) UIFont *fontPlaceholder;

/// 默认文字的颜色
@property (nonatomic,strong) UIColor *colorPlaceholder;


@end


@implementation MPCTextField


#pragma mark - ********************************** 初始化方法 *****************************************

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        /// 设置定制属性，与APP的UI规范 有关
        /// [self setCustomAttribute]; /// 暂时不用
    }
    return self;
}

/// 设置定制属性，与APP的UI规范 有关
- (void)setCustomAttribute
{
    if (self)
    {
        /// 设置textField的样式
        self.backgroundColor = [UIColor clearColor];
        self.contentMode = UIViewContentModeCenter;
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
        self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.font = [UIFont systemFontOfSize:15.0];
        self.textColor = [UIColor blackColor];
    }
}

/// 重写方法，设置占位文字的默认颜色和大小
- (void)setPlaceholder:(NSString *)placeholder
{
    [super setPlaceholder:placeholder];
    /// 如果存在placeholder
    if (placeholder)
    {
        UIColor *colorPlaceholder = [self gmColorWithHexString:@"0x999999"];
        UIFont *fontPlaceholder = [UIFont systemFontOfSize:15.0];
        
        _fontPlaceholder = fontPlaceholder;
        _colorPlaceholder = colorPlaceholder;
        
        /// 设置占位文字默认的样式
        NSDictionary *dicAttri = @{
                                   NSForegroundColorAttributeName : colorPlaceholder,
                                   NSFontAttributeName : fontPlaceholder
                                   };
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder
                                                                     attributes:dicAttri];
    }
}

/// 通过16进制RBG值获得相应的颜色值，如：0xAABBCC、#AABBCC
- (UIColor *)gmColorWithHexString:(NSString *)strRGB
{
    /// 去掉空格，并全改为大写形式，便于下面的计算
    NSString *strColor = [[self tpSpaceTrimAll:strRGB] uppercaseString];
    
    /// 字符串必须大于6位，否则默认返回黑色
    if ([strColor length] < 6)
    {
        return [UIColor blackColor];
    }
    
    ///  strip 0X if it appears
    if ([strColor hasPrefix:@"0X"])
    {
        strColor = [strColor substringFromIndex:2];
    }
    else if ([strColor hasPrefix:@"#"])
    {
        strColor = [strColor substringFromIndex:1];
    }
    
    /// 剩下的位数一定是6位，否则默认返回黑色
    if ([strColor length] != 6)
    {
        return [UIColor blackColor];
    }
    
    /// 分别获取R、G、B的值
    NSRange range;
    range.location = 0;
    range.length = 2;
    NSString *rString = [strColor substringWithRange:range];
    
    range.location = 2;
    NSString *gString = [strColor substringWithRange:range];
    
    range.location = 4;
    NSString *bString = [strColor substringWithRange:range];
    
    /// Scan values
    unsigned int cuintR;
    unsigned int cuintG;
    unsigned int cuintB;
    [[NSScanner scannerWithString:rString] scanHexInt:&cuintR];
    [[NSScanner scannerWithString:gString] scanHexInt:&cuintG];
    [[NSScanner scannerWithString:bString] scanHexInt:&cuintB];
    
    return [UIColor colorWithRed:((float) cuintR / 255.0f)
                           green:((float) cuintG / 255.0f)
                            blue:((float) cuintB / 255.0f)
                           alpha:1.0f];
}

- (NSString *)tpSpaceTrimAll:(NSString *)str
{
    /// 将所有空格替换为空字符
    NSString *strResult = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    /// 加入nil保护
    return (strResult) ? (strResult) : (@"");
}


#pragma mark - ********************************** 初始化方法 工厂模式 *********************************

/// 创建一个普通的UItextField
+ (MPCTextField *)getWithFrame:(CGRect)frame
                       target:(id)target
{
    MPCTextField *textField = [[MPCTextField alloc] initWithFrame:frame];
    textField.delegate = target;
    
    return textField;
}

/// 创建一个普通的 UItextField 有默认的占位文字
+ (MPCTextField *)getWithFrame:(CGRect)frame
                       target:(id)target
                  placeholder:(NSString *)strPlaceholder
{
    MPCTextField *textField = [MPCTextField getWithFrame:frame target:target];
    
    /// 设置占位文字
    textField.placeholder = strPlaceholder;
    
    return textField;
}

#pragma mark - ********************************** 设置样式的 对象方法 *********************************

- (instancetype)initWithFrame:(CGRect)frame target:(id)target
{
    if (self = [self initWithFrame:frame])
    {
        self.delegate = target;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame target:(id)target placeholder:(NSString *)placeholder
{
    if (self = [self initWithFrame:frame])
    {
        self.delegate = target;
        
        self.placeholder = placeholder;
    }
    return self;
}

/// 设置边框和圆角
-(void)setCornerRadius:(CGFloat)fltCornerRadius borderWidth:(CGFloat)fltBorderWidth borderColor:(UIColor *)colorBorder
{
    
    self.layer.cornerRadius = fltCornerRadius;
    self.layer.borderColor = [colorBorder CGColor];
    self.layer.borderWidth = fltBorderWidth;
    self.clipsToBounds = YES;
}

/// 设置占位文字的大小
-(void)setPlaceholderFont:(UIFont *)fontPlaceholder
{
    if (self.placeholder && fontPlaceholder)
    {
        /// 保存文字大小
        _fontPlaceholder = fontPlaceholder;
        
        /// 设置占位文字的样式
        NSDictionary *dicAttri = @{
                                   NSForegroundColorAttributeName : _colorPlaceholder,
                                   NSFontAttributeName : fontPlaceholder
                                   };
        
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder
                                                                     attributes:dicAttri];
    }
}

/// 设置占位文字的颜色
-(void)setPlaceholderColor:(UIColor *)colorPlaceholder
{
    if (self.placeholder && colorPlaceholder)
    {
        /// 保存文字颜色
        _colorPlaceholder = colorPlaceholder;
        
        /// 设置占位文字的样式
        NSDictionary *dicAttri = @{
                                   NSForegroundColorAttributeName : colorPlaceholder,
                                   NSFontAttributeName : _fontPlaceholder
                                   };
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder
                                                                     attributes:dicAttri];
    }
}


@end



